package game{
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	
	import mx.collections.ArrayCollection;
	import mx.controls.*;
	import mx.core.UIComponent;
	
	public class Game extends EventDispatcher{
		private static var STATE_UNINITIALIZED:int = 0;
		private static var STATE_MENU:int = 1;
		private static var STATE_RUN:int = 2;
		
		private var state:int = STATE_UNINITIALIZED;
		
		private static var LEFT:int = 37;
		private static var UP:int = 38;
		private static var RIGHT:int = 39;
		private static var FIRE:int = 32;
		
		private static var W:int = 87;
		private static var S:int = 83;
		private static var A: int = 65;
		private static var D: int = 68;
		
		private static var FRAME_RATE:int = 30;
		
		private var timer:Timer;
		private var counter:int;
		
		private var buffer:BitmapData;
		private var canvas:UIComponent;
		private var spaceShip:SpaceShip;
		private var gameObjects:ArrayCollection = new ArrayCollection();;
		private var lastTickTime:int;
		
		private const ROTATE_STEP:Number = 10;
		
		private var rotateClockwise:Boolean = false;
        private var rotateAntiClockwise:Boolean = false;
        private var fire:Boolean = false; 
		private var maxD:Number = 0;
		private var avD:Number = 0;
		private var totalT:Number = 0;
		public function setScreen(screen:UIComponent):void{
			this.canvas = screen;
			buffer = new BitmapData(screen.width, screen.height, false);
			
			spaceShip = new SpaceShip(this);
			spaceShip.x = screen.width / 2;
			spaceShip.y = screen.height / 2;
			
			addGameObject(spaceShip);
		}

		private function addGameObject(object:GameObject):void{
			gameObjects.addItem(object);
			canvas.stage.addChild(object);
		}
		
		public function Game(){
			
		}
		
		private function tick(event:TimerEvent):void{
//			trace("调用间隔:",getTimer()-lastTickTime,"毫秒");
			
//			var s:int = getTimer();
			this.canvas.setFocus();
			input();
			run();
			paint();
			
//			var t:int = getTimer()-s;
//			counter++;
//			totalT += t;
//			maxD = Math.max(maxD , t);
//			avD = totalT / counter;
//			trace("最长时间",maxD,"刷新次数",counter,"平均时间",avD,"毫秒");
		}
		
		private function input():void{
			if(rotateClockwise){
				spaceShip.rotate(ROTATE_STEP);
			}
			if(rotateAntiClockwise){
				spaceShip.rotate(-ROTATE_STEP);
			}
			if(fire){
				spaceShip.fire();
			}
		}
		
		private function run():void{
			var now:int = getTimer();
			for each (var gameObject:GameObject in gameObjects){
			    gameObject.tick(now - lastTickTime);
			}
			
			lastTickTime = now;
		}
		
		private function paint():void{
			if(!canvas) return;

			var g:Graphics = canvas.graphics;
			
			g.clear();
			
			g.beginFill(0,1);
			g.drawRect(0,0,canvas.width,canvas.height);
			g.endFill();
			
			g.lineStyle(1,0xffffff);
			
			g.moveTo(0,canvas.height /2);
			g.lineTo(canvas.width,canvas.height /2);
			
			g.moveTo(canvas.width /2,0);
			g.lineTo(canvas.width /2,canvas.height);
			
			for each (var gameObject:GameObject in gameObjects){
			    gameObject.drawToBuffer(buffer);
			}
			
//			screen.graphics.clear();
//			
//			screen.graphics.beginBitmapFill(buffer,null,false,false);
//			screen.graphics.drawRect(0, 0, screen.width, screen.height);
//			screen.graphics.endFill();
//			
//			screen.graphics.lineStyle(1,0xfffff);
//			
//			screen.graphics.moveTo(0,screen.height /2);
//			screen.graphics.lineTo(screen.width,screen.height /2);
//			
//			screen.graphics.moveTo(screen.width /2,0);
//			screen.graphics.lineTo(screen.width /2,screen.height);
			
//			screen.graphics.lineStyle(1,0xff00);
//			var path:ArrayCollection = spaceShip.path;
//			for(var i:int = 0; i<path.length -1;i++){
//				var p1:Point = Point(path.getItemAt(i));
//				var p2:Point = Point(path.getItemAt(i+1));
//				
//				screen.graphics.moveTo(p1.x,p1.y);
//				screen.graphics.lineTo(p2.x,p2.y);
//			}
			
			
		}
		
        public function keyReleased(event:KeyboardEvent):void{
        	switch(event.keyCode){
        		case UP:
        			spaceShip.powerDown();
        			break;
        		case LEFT:
        			rotateAntiClockwise = false;
        			break;
        		case RIGHT:
        			rotateClockwise = false;
        			break;
        		case FIRE:
        			fire = false;
        			break;
        	}
        }  
        
        public function keyPressed(event:KeyboardEvent):void{
//        	trace("keyCode="+event.keyCode);
        	switch(event.keyCode){
        		case UP:
        			spaceShip.powerUp();
        			break;
        		case LEFT:
        			rotateAntiClockwise = true;
        			break;
        		case RIGHT:
        			rotateClockwise = true;
        			break;
        		case FIRE:
        			fire = true;
        			break;
        	}
		}  
		
		public function start():void{
			stop();
			
			lastTickTime = getTimer();
			
			timer = new Timer(1000 / FRAME_RATE);
			timer.addEventListener(TimerEvent.TIMER,tick);
			timer.start();
		}
		
		public function stop():void{
			if(timer) timer.stop();
		}
		
		public function destroy():void{
			
		}
		
		public function getScreenWidth():int{
			if(canvas){
				return canvas.width;
			}else{
				return 0;
			}
		}
		
		public function getScreenHeight():int{
			if(canvas){
				return canvas.height;
			}else{
				return 0;
			}
		}
	}
}